package com.wt.springsamples.es.lucene;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;
import java.nio.file.Paths;


public class LuceneSearchTest {
    public static void main(String[] args) throws IOException, ParseException {
        Directory directory = FSDirectory.open(Paths.get("d:\\lucene\\index"));
        Analyzer analyzer = new StandardAnalyzer();
        DirectoryReader reader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(reader);
        // Parse a simple query that searches for "text":
        QueryParser parser = new QueryParser("name", analyzer);
        Query query = parser.parse("texted");
        ScoreDoc[] hits = indexSearcher.search(query, 4096).scoreDocs;
        // Iterate through the results:
        for (int i = 0; i < hits.length; i++) {
            Document hitDoc = indexSearcher.doc(hits[i].doc);
            System.out.println(i+"======>"+hitDoc.get("name"));
        }
        reader.close();
        directory.close();
    }
}
